using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace CocaCola_Inventory_System.Forms
{
    public partial class ExportToExcelOrder : Form

    {
        string file = "";
        bool success;
        
        private System.Data.DataTable table;
        private System.Data.DataTable Columns;
        private System.Data.DataTable data;
        public ExportToExcelOrder()
        {
            InitializeComponent();
            Columns = new System.Data.DataTable();
            Columns.Columns.Add("ColName");
        }

        private void ExportToExcelOrder_Load(object sender, EventArgs e)
        {
          
        }
        public System.Data.DataTable ExportTable
        {
            set
            {
                data = value;
                foreach(DataColumn c in data.Columns)
                    Columns.Rows.Add(c.ColumnName);
                ColList.DataSource = Columns;
                ColList.DisplayMember = "ColName";
                ColList.ValueMember = "ColName";
                
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int index = GetSelectedIndex();
                if (index < 1)
                    return;

                DataRow r = Columns.Rows[index];
                DataRow r2 = Columns.Rows[index - 1];
                //r.Delete();
                //r2.Delete();
                object[] o = r.ItemArray;
                object[] o2 = r2.ItemArray;

                //Columns.Rows.InsertAt(r, index - 1);
                //Columns.Rows.InsertAt(r2, index);
                r.ItemArray = o2;
                r2.ItemArray = o;
                ColList.DataSource = Columns;
                ColList.DisplayMember = "ColName";
                ColList.ValueMember = "ColName";
                ColList.SelectedIndex--;
                ColList.Update();
            }
            catch (Exception ex)
            {

                return;
            }
            

        }
        private int GetSelectedIndex()
        {
            return ColList.SelectedIndex;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Data.DataTable datasorted = new System.Data.DataTable();
            foreach (DataRow r in Columns.Rows)
                datasorted.Columns.Add(r[0].ToString());
            foreach (DataRow r in data.Rows)
            {
                object[] o = new object[datasorted.Columns.Count];
                for (int i = 0; i < Columns.Rows.Count; i++)
                    o[i] = r[Columns.Rows[i][0].ToString()];
                datasorted.Rows.Add(o);
            }
            table = datasorted;
            DialogResult res = saveFileDialog1.ShowDialog();
            if (res != DialogResult.OK)
                return;
            file = saveFileDialog1.FileName;

            this.Enabled = false;
            toolStripProgressBar1.Visible = true;
            toolStripProgressBar1.Value = 0 ;
            SaveWorker.RunWorkerAsync();
        }

        private void SaveWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                Workbook work = excel.Application.Workbooks.Add(System.Reflection.Missing.Value);

                int ColumnIndex = 0;
                foreach (DataColumn col in table.Columns)
                {
                    ColumnIndex++;
                    excel.Cells[1, ColumnIndex] = col.ColumnName;

                }
                int rowIndex = 0;
                foreach (DataRow row in table.Rows)
                {
                    rowIndex++;
                    ColumnIndex = 0;
                    foreach (DataColumn col in table.Columns)
                    {
                        ColumnIndex++;
                        excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName].ToString();
                    }
                    SaveWorker.ReportProgress(rowIndex * 100 / table.Rows.Count);
                }
                //excel.Visible = true; 
                //Worksheet worksheet = (Worksheet)excel.ActiveSheet; 
                //worksheet.Activate();
                excel.ScreenUpdating = false;
                excel.DisplayAlerts = false;

                //work.SaveCopyAs(file);
                work.SaveCopyAs(file);
                excel.Quit();

                MessageBox.Show("Report Exported SuccessFully");
                success = true;

            }
            catch (Exception ex)
            {
                success = false;
                MessageBox.Show("Failed To Export Report");
            }
        }
        private void SaveWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            toolStripProgressBar1.Value = e.ProgressPercentage;   
        }

        private void SaveWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Enabled = true;
            toolStripProgressBar1.Visible = false;
            toolStripProgressBar1.Value = 0 ;
            if (success)
                this.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int index = GetSelectedIndex();
                if (index > Columns.Rows.Count-2)
                    return;

                DataRow r = Columns.Rows[index];
                DataRow r2 = Columns.Rows[index+1 ];
                //r.Delete();
                //r2.Delete();
                object[] o = r.ItemArray;
                object[] o2 = r2.ItemArray;

                //Columns.Rows.InsertAt(r, index - 1);
                //Columns.Rows.InsertAt(r2, index);
                r.ItemArray = o2;
                r2.ItemArray = o;
                ColList.DataSource = Columns;
                ColList.DisplayMember = "ColName";
                ColList.ValueMember = "ColName";
                ColList.SelectedIndex++;
                ColList.Update();
            }
            catch (Exception ex)
            {

                return;
            }
        }
    }
}